<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class home extends core {
	public $goods_mod;

	public function __construct() {
		parent::__construct();
		$this->goods_mod = m('goods');
	}

	//首页接口
	public function index() {
		$flashes = $this->_flashes();
		$grids = $this->_grids();
		$articles = $this->_articles();

		//微信端登录,检查用户有没有绑定手机号码
        $member = SQL::share('member')->where("id='{$this->member_id}'")->row();
        if ($member){
            $member_login = 1;
            if ($member->mobile){
                //绑定等于1
                $mobile = 1;
            }else{
                $mobile = 0;
            }
            success(array("flashes"=>$flashes, "grids"=>$grids, "articles"=>$articles, "mobile"=>$mobile, "member_login"=>$member_login));
        }else{
            $member_login = 0;
            success(array("flashes"=>$flashes, "grids"=>$grids, "articles"=>$articles, "member_login"=>$member_login));
        }

	}

	//幻灯广告
	private function _flashes() {
		$now = time();
		$sql = "SELECT * FROM {$this->tbp}ad WHERE position='flash' AND status='1'
			ORDER BY sort ASC, id DESC LIMIT 5";
		$ads = $this->db->get_results($sql);
		if ($ads) {
			$ads = add_domain_deep($ads, array("pic"));
		}
		return $ads;
	}

	//商品分类
	private function _categorys() {
		$sql = "SELECT id, name, pic FROM {$this->tbp}goods_category WHERE status='1' AND parent_id=0 ORDER BY sort ASC, id DESC";
		$rs = $this->db->get_results($sql);
		if ($rs) {
			$rs = add_domain_deep($rs, array("pic"));
		}
		return $rs;
	}

	//推荐
	private function _grids() {
		$rs = SQL::share('article')->where("status=1 AND is_system=0 AND LOCATE(',1,', CONCAT(',',ext_property,','))>0")
			->pagesize(4)->sort('id DESC')->find('id, title, pic, memo', function($rs){
			foreach ($rs as $k=>$g) {
				$rs[$k]->title = mb_substr($g->title, 0, 16);
				$rs[$k]->memo = mb_substr($g->memo, 0, 18);
			}
			return $rs;
		});
		return $rs;
	}

	//热门
	private function _articles() {
		$rs = SQL::share('article')->where("status=1 AND is_system=0 AND LOCATE(',2,', CONCAT(',',ext_property,','))>0")
			->pagesize(6)->sort('id DESC')->find('id, title');
		if ($rs) {
			foreach ($rs as $k=>$g) {
				$rs[$k]->title = mb_substr($g->title, 0, 26);
			}
		}
		return $rs;
	}
	
	//参保地
	public function province() {
		$rs = SQL::share('service_province sp')
			->left('province p', 'sp.province_id=p.province_id')
			->where('status=1')->sort('sp.id ASC')->find('sp.*, p.name');
		success($rs);
	}
	public function city() {
		$province_id = $this->request->get('province_id', 0);
		if ($province_id<=0) error('数据错误');
		$rs = SQL::share('service_city sc')
			->left('city c', 'sc.city_id=c.city_id')
			->left('service_province sp', 'sp.id=sc.parent_id')
			->left('province p', 'sp.province_id=p.province_id')
			->where("sp.province_id='{$province_id}'")->sort('sc.id ASC')->find('sc.*, c.name, sp.province_id, p.name as province_name');
		success($rs);
	}
	
	//计算器
	public function casy() {
		success('ok');
	}
	public function get_casy() {
		$city_id = $this->request->get('city_id', 0);
		if ($city_id<=0) error('数据错误');
		$rs = SQL::share('service_casy sa')
			->left('service_city sc', 'service_city_id=sc.id')
			->where("sc.city_id='{$city_id}'")->sort('sa.id ASC')->find('sa.*');
		$row = SQL::share('service_city')->where("city_id='{$city_id}'")->row();
		success(array('casy'=>$rs, 'row'=>$row));
	}
	public function casy_detail() {
		$province_id = $this->request->post('province_id', 0);
		$city_id = $this->request->post('city_id', 0);
		$casy_id = $this->request->post('casy_id', 0);
		$gjj_value = $this->request->post('gjj', 0);
		if ($province_id<=0 || $city_id<=0 || $casy_id<=0) error('数据错误');
		$name = SQL::share('service_casy')->where($casy_id)->val('name');
		$rs = SQL::share('service_casy_detail')->where("casy_id='{$casy_id}'")->sort('id ASC')
			->find('*, TRUNCATE(money*(firm/100),2) as firm_money, TRUNCATE(money*(person/100),2) as person_money');
		
		$total_price = $total = $gjj_total = $firm_total = $person_total = 0;
		if ($rs) {
			foreach ($rs as $k=>$g) {
				$firm_total = bcadd($firm_total, $g->firm_money, 2);
				$person_total = bcadd($person_total, $g->person_money, 2);
			}
			
			$casy = SQL::share('service_city sc')->left('service_casy c', 'sc.id=service_city_id')
				->where("c.id='{$casy_id}'")->row('sc.money, sc.firm, sc.person');
			$gjj = new stdClass();
			$gjj->name = '公积金';
			//$gjj->money = $casy->money;
			$gjj->money = $gjj_value;
			$gjj->firm = $casy->firm;
			$gjj->person = $casy->person;
			$gjj->firm_money = strval(round($gjj->money*($gjj->firm/100),2));
			$gjj->person_money = strval(round($gjj->money*($gjj->person/100),2));
			$rs[] = $gjj;
			
			$total = bcadd($firm_total, $person_total, 2);
			$gjj_total = bcadd($gjj->firm_money, $gjj->person_money, 2);
			$total_price = bcadd($total, $gjj_total, 2);
		}
		success(array('rs'=>$rs, 'name'=>$name,
			'total_price'=>$total_price, 'total'=>$total, 'gjj_total'=>$gjj_total, 'firm_total'=>$firm_total, 'person_total'=>$person_total));
	}

	//搜索历史
	public function search_history() {
		$history = $hot = array();
		if ($this->member_id) {
			$sql = "SELECT * FROM {$this->tbp}seach_history WHERE member_id='{$this->member_id}' ORDER BY id DESC LIMIT 20";
			$rs = $this->db->get_results($sql);
			if ($rs) {
				foreach ($rs as $k=>$g) {
					$history[] = $g->content;
				}
			}
		}
		if (isset($this->configs['hot_search']) && strlen(trim($this->configs['hot_search']))) {
			$arr = explode(',', $this->configs['hot_search']);
			foreach ($arr as $g) {
				$hot[] = trim($g);
			}
		}
		success(array('history'=>$history, 'hot'=>$hot));
	}
	
	//清空搜索历史
	public function clear_search_history() {
		$sql = "DELETE FROM {$this->tbp}seach_history WHERE member_id='{$this->member_id}'";
		$this->db->query($sql);
		success('ok');
	}

	//删除搜索历史
	public function delete_search_history() {
		$content = (isset($_GET['content']) && trim($_GET['content'])) ? trim($_GET['content']) : '';
		$sql = "DELETE FROM {$this->tbp}seach_history WHERE member_id='{$this->member_id}' AND content='{$content}'";
		$this->db->query($sql);
		success('ok');
	}
}
